﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace DBAccessLibrary.Common
{
    [Serializable]
    public class BasePropertyChange : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged 成员
        [field: NonSerializedAttribute]
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void RegistProperty(string funcname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(funcname));
            }
        }
    }

    public static class PropertyChangedEx
    {
        public static void NotifyPropertyChanged<T, TProperty>(this T entity, Expression<Func<T, TProperty>> func) where T : BasePropertyChange
        {
            MemberExpression member = func.Body as MemberExpression;
            if (null != member)
            {
                entity.RegistProperty(member.Member.Name);
            }
            else
            {
                throw new Exception("表达式 必须是属性形式");
            }
        }
    }
}
